Skip to content

[python tests] Fix determine-reboot-cause_test.py importing module sonic_platform#359

Open
judsonwilson-nvidia wants to merge 1 commit intosonic-net:masterfrom
judsonwilson-nvidia:judsonw-fix-determine-reboot-cause-sonic-platform
Open

[python tests] Fix determine-reboot-cause_test.py importing module sonic_platform#359
judsonwilson-nvidia wants to merge 1 commit intosonic-net:masterfrom
judsonwilson-nvidia:judsonw-fix-determine-reboot-cause-sonic-platform

Conversation

@judsonwilson-nvidia
Copy link

This test, determine-reboot-cause_test.py and the script it tests, scripts/determine-reboot-cause expect the module sonic_platform to not exist on the system during unit tests on build hosts. The script implementation handles this situation by trying to import sonic_platform in the funciton calls, and having them return fake values in an ImportError exception handler. Otherwise, if sonic_platform is present in the build system when running unit tests, it will throw exceptions because the platform code is designed to run an a real SONiC device host.

However, on the build system, there are races in the build, and it is possible for sonic_platform to be installed and loaded. This will break this test.

This PR adds a fixture to remove the module and block it using temporary monkeypatching. None is assigned to system.modules['sonic_platform'] to evict any cached loaded modules and block future loading, until the test is complete.

This PR is in response to the following unit test failures appearing during builds on some occasions:

  FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_find_hardware_reboot_cause_not_installed_or_not_implemented - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
  FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_determine_reboot_cause_main_without_reboot_cause_dir - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
  FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_determine_reboot_cause_main_with_reboot_cause_dir - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'

With details similar to:

  _ TestDetermineRebootCause.test_find_hardware_reboot_cause_not_installed_or_not_implemented _

  self = <tests.determine-reboot-cause_test.TestDetermineRebootCause object at 0x7f9c4a940d50>

      def test_find_hardware_reboot_cause_not_installed_or_not_implemented(self):
  >       result = determine_reboot_cause.find_hardware_reboot_cause()

  tests/determine-reboot-cause_test.py:140:
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  scripts/determine-reboot-cause:127: in find_hardware_reboot_cause
      hardware_reboot_cause_major, hardware_reboot_cause_minor = get_reboot_cause_from_platform()
  scripts/determine-reboot-cause:115: in get_reboot_cause_from_platform
      platform  = sonic_platform.platform.Platform()
  /usr/local/lib/python3.13/dist-packages/sonic_platform/platform.py:34: in __init__
      if DeviceDataManager.get_dpu_count():
  /usr/local/lib/python3.13/dist-packages/sonic_platform/utils.py:183: in _impl
      method.return_value = method(*args, **kwargs)
  /usr/local/lib/python3.13/dist-packages/sonic_platform/device_data.py:372: in get_dpu_count
      dpu_data = cls.get_platform_dpus_data()
  /usr/local/lib/python3.13/dist-packages/sonic_platform/utils.py:183: in _impl
      method.return_value = method(*args, **kwargs)
  /usr/local/lib/python3.13/dist-packages/sonic_platform/device_data.py:357: in get_platform_dpus_data
      platform_path = device_info.get_path_to_platform_dir()
  /usr/local/lib/python3.13/dist-packages/sonic_py_common/device_info.py:283: in get_path_to_platform_dir
      platform_path_host = os.path.join(HOST_DEVICE_PATH, platform)
  <frozen posixpath>:90: in join
      ???
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

  funcname = 'join', args = ('/usr/share/sonic/device', None), hasstr = True
  hasbytes = False, s = None

  >   ???
  E   TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'

  <frozen genericpath>:188: TypeError

@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@judsonwilson-nvidia judsonwilson-nvidia force-pushed the judsonw-fix-determine-reboot-cause-sonic-platform branch from e12b57a to 78c6d46 Compare March 17, 2026 07:50
@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

…nic_platform

This test, `determine-reboot-cause_test.py` and the script it tests,
`scripts/determine-reboot-cause` expect the module `sonic_platform` to not exist
on the system during unit tests on build hosts. The script implementation
handles this situation by trying to import `sonic_platform` in the function
calls, and having them return fake values in an `ImportError` exception handler.
Otherwise, if `sonic_platform` is present in the build system when running unit
tests, it will throw exceptions because the platform code is designed to run an
a real SONiC device host.

However, on the build system, there are races in the build, and it is possible
for `sonic_platform` to be installed and loaded. This will break this test.

This PR adds a fixture to remove the module and block it using a temporary
monkeypatch. `None` is assigned to `system.modules['sonic_platform']` to evict
any cached loaded modules and block future loading, until the test is complete.

This PR is in response to the following unit test failures appearing during
builds on some occasions:

```
  FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_find_hardware_reboot_cause_not_installed_or_not_implemented - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
  FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_determine_reboot_cause_main_without_reboot_cause_dir - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
  FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_determine_reboot_cause_main_with_reboot_cause_dir - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
```

With details similar to:

```
  _ TestDetermineRebootCause.test_find_hardware_reboot_cause_not_installed_or_not_implemented _

  self = <tests.determine-reboot-cause_test.TestDetermineRebootCause object at 0x7f9c4a940d50>

      def test_find_hardware_reboot_cause_not_installed_or_not_implemented(self):
  >       result = determine_reboot_cause.find_hardware_reboot_cause()

  tests/determine-reboot-cause_test.py:140:
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  scripts/determine-reboot-cause:127: in find_hardware_reboot_cause
      hardware_reboot_cause_major, hardware_reboot_cause_minor = get_reboot_cause_from_platform()
  scripts/determine-reboot-cause:115: in get_reboot_cause_from_platform
      platform  = sonic_platform.platform.Platform()
  /usr/local/lib/python3.13/dist-packages/sonic_platform/platform.py:34: in __init__
      if DeviceDataManager.get_dpu_count():
  /usr/local/lib/python3.13/dist-packages/sonic_platform/utils.py:183: in _impl
      method.return_value = method(*args, **kwargs)
  /usr/local/lib/python3.13/dist-packages/sonic_platform/device_data.py:372: in get_dpu_count
      dpu_data = cls.get_platform_dpus_data()
  /usr/local/lib/python3.13/dist-packages/sonic_platform/utils.py:183: in _impl
      method.return_value = method(*args, **kwargs)
  /usr/local/lib/python3.13/dist-packages/sonic_platform/device_data.py:357: in get_platform_dpus_data
      platform_path = device_info.get_path_to_platform_dir()
  /usr/local/lib/python3.13/dist-packages/sonic_py_common/device_info.py:283: in get_path_to_platform_dir
      platform_path_host = os.path.join(HOST_DEVICE_PATH, platform)
  <frozen posixpath>:90: in join
      ???
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

  funcname = 'join', args = ('/usr/share/sonic/device', None), hasstr = True
  hasbytes = False, s = None

  >   ???
  E   TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'

  <frozen genericpath>:188: TypeError
```

Signed-off-by: Judson Wilson <judsonw@nvidia.com>
@judsonwilson-nvidia judsonwilson-nvidia force-pushed the judsonw-fix-determine-reboot-cause-sonic-platform branch from 78c6d46 to de46066 Compare March 17, 2026 22:41
@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants